package com.artron.mediaartron.ui.fragment.general;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.mediaartron.R;
import com.artron.mediaartron.ui.activity.GeneralActivity;

import butterknife.BindView;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-13.
 */

public class TextEditFragment extends BaseStaticFragment {

    @BindView(R.id.TextEditFragment_et_content)
    protected EditText mEtText;
    private String mContent;

    public static TextEditFragment newInstance(String content) {
        TextEditFragment fragment = new TextEditFragment();
        fragment.init(content);
        return fragment;
    }

    private void init(String content) {
        this.mContent = content;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_text_edit;
    }

    @Override
    protected void initView() {
        if (!TextUtils.isEmpty(mContent)) {
            mEtText.setText(mContent);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_save:
                if (mEtText != null) {
                    String text = mEtText.getText().toString();
                    if (!TextUtils.isEmpty(text)) {
                        Intent intent = new Intent();
                        intent.putExtra(GeneralActivity.CONTENT, text);
                        getActivity().setResult(GeneralActivity.RESULT_OK, intent);
                    }
                }
                getActivity().onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
